package com.dq.controller.serviceproject;

import com.alibaba.fastjson.JSONObject;
import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.ServiceProjectCategoryQuery;
import com.dq.vo.Vo;
import com.dq.domain.service.ServiceProjectCategory;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cf
 */
@Api(description = "服务项目类别")
@RestController
@RequestMapping("sys/boss/serviceProjectCategory/")
public class ServiceProjectCategoryController extends AllService {

    @ApiOperation("获取所有项目类别 TREE， 或根据 服务项目 获取类别")
    @PostMapping("/listTree")
    @RequiresPermissions("sys:serviceProjectCategory:list")
    public Vo list(@ApiParam("") @RequestBody ServiceProjectCategoryQuery serviceProjectCategoryQuery) throws AdminException {

        List<ServiceProjectCategory.Vo> vos = serviceProjectCategoryService.listByPage(serviceProjectCategoryQuery);

        JSONObject json = new JSONObject();
        json.put("projectCategoryTree", vos);
//        json.put("projectCategory", VO.toVOCollection(categoryList));
        return new Vo(json);
    }

    @ApiOperation("添加")
    @PostMapping("/save/{parentId}")
    @RequiresPermissions("sys:serviceProjectCategory:save")
    public Vo list(@RequestBody ServiceProjectCategory serviceProjectCategory, @PathVariable @ApiParam("上级id") Long parentId) throws AdminException {

        List<ServiceProjectCategory> list = serviceProjectCategoryRepository.findByDeletedAndCategoryName(false, serviceProjectCategory.getCategoryName());
        if(list != null && list.size() > 0) {
            throw new AdminException("此类别已存在");
        }
        if(parentId != null && parentId != -1) {
            ServiceProjectCategory parent = dao.get(ServiceProjectCategory.class, parentId);
            if (null == parent) {
                throw new AdminException("上级不存在");
            }
            serviceProjectCategory.setParent(parent);
        }

        ServiceProjectCategory save = serviceProjectCategoryService.save(serviceProjectCategory);

        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:serviceProjectCategory:update")
    public Vo update(@RequestBody ServiceProjectCategory serviceProjectCategory) throws AdminException {
        if(serviceProjectCategory.getId() == null) {
            throw new AdminException("缺参id");
        }

        ServiceProjectCategory serviceProjectCategoryQuery = dao.get(ServiceProjectCategory.class, serviceProjectCategory.getId());
        if (serviceProjectCategoryQuery == null) {
            throw new AdminException("无对应id对象");
        }
        if(StringUtils.isNotBlank(serviceProjectCategory.getCategoryName())) {
            if(!serviceProjectCategoryQuery.getCategoryName().equals(serviceProjectCategory.getCategoryName())) {
                List<ServiceProjectCategory> nameList = serviceProjectCategoryRepository.findByDeletedAndCategoryName(false, serviceProjectCategory.getCategoryName());
                if(nameList != null && nameList.size() > 0) {
                    throw new AdminException("此项目已存在");
                }
            }
        }

        BeanUtils.copyProperties(serviceProjectCategory, serviceProjectCategoryQuery, BeanNullUtil.getNullPropertyNames(serviceProjectCategory));
        serviceProjectCategoryService.update(serviceProjectCategoryQuery);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @SysLog("删除")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:serviceProjectCategory:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        ServiceProjectCategory serviceProjectCategory = dao.get(ServiceProjectCategory.class, id);
        if (serviceProjectCategory == null) {
            throw new AdminException("无对应id对象");
        }
        List<ServiceProjectCategory> sons = serviceProjectCategoryRepository.findByParentAndDeletedFalse(serviceProjectCategory);
        if (!sons.isEmpty()) {
            throw new AdminException("请先删除子级对象");
        }

        serviceProjectCategory.setDeleted(true);
        serviceProjectCategoryService.update(serviceProjectCategory);
        return new Vo(HttpStatus.OK);
    }

}
